# #V1.0
# money=int(input("请输入你的账户余额："))
# shoppinglist=[{"name":"卫生纸","price":19},{"name":"矿泉水","price":2},{"name":"洗发水","price":30},{"name":"暖水壶","price":59},{"name":"雷碧","price":3},{"name":"康帅傅方便面","price":5},{"name":"奶龙","price":888}]
# #购物车
# shopping_cart=[]
# #打印购物小条
# def receipt():
#     print("----------购物小条----------")
#     for item in shopping_cart:
#         print(f"商品：{item['name']},价格：{item['price']}")
# #购物系统
# def shopping():
#     global money
#     while True:
#         print("----------我们有以下商品----------")
#         for index,item in enumerate(shoppinglist): #tip:enumerate()将列表组合为一个序列索引，同时列出数据和下标
#             print(f"{index+1}.{item['name']}-{item['price']}元")
#         choice=input("你要啥子东西？请输入商品编号（输入q,Q退出：")
#         if choice=="q" or choice=="Q":
#             print("不买东西别进来昂，不然装X让你飞起来")
#             break
#         try:
#             index=int(choice)-1
#             if 0<=index<len(shoppinglist):
#                 good=shoppinglist[index]
#                 if good["price"]<=money:
#                     shopping_cart.append(good)
#                     print(f"good['name']成功添加到购物车！")
#                     money-=good["price"]
#                     print(f"恭喜，购买成功！您的卡余额还剩{money}")
#                 else:
#                     print("穷鬼，钱不够！请到其他超市购买")
#             else:
#                 print("没有这个商品，别瞎弄！")
#         except ValueError:
#             print("无效输入，再瞎搞让你飞出去！")
# shopping()
# receipt()

#V2.0
import datetime
import random
consumption=0
money=int(input("请输入你的账户余额："))
shoppinglist=[{"name":"卫生纸","price":19},{"name":"矿泉水","price":2},{"name":"洗发水","price":30},{"name":"暖水壶","price":59},{"name":"雷碧","price":3},
              {"name":"康帅傅方便面","price":5},{"name":"奶龙","price":888},{"name":"冰箱","price":2699},{"name":"五粮液","price":666},{"name":"衣架","price":29}]
#购物车
shopping_cart=[]
#打印购物小条
def receipt():
    count = len(shopping_cart)
    time = datetime.datetime.now()
    total_cost=sum(good["price"] for good in shopping_cart)
    print("----------购物小条----------")
    for item in shopping_cart:
        print(f"商品：{item['name']},价格：{item['price']}")
    print(f"购物时间-----{time}")
    print(f"卡余额-----{money}")
    print(f"购物数量-----{count}")
    print(f"总消费-----{total_cost}")
    print("---------------------------")
#购物系统
def shopping():
    global money
    while True:
        print("----------我们有以下商品----------")
        for index,item in enumerate(shoppinglist): #tip:enumerate()将列表组合为一个序列索引，同时列出数据和下标
            print(f"{index+1}.{item['name']}-{item['price']}元")
        choice=input("你要啥子东西？请输入商品编号（输入q,Q退出):")
        if choice=="q" or choice=="Q":
            break
        try:
            index=int(choice)-1
            if 0<=index<len(shoppinglist):
                good=shoppinglist[index]
                if choose_coupon["name"]==good["name"]:
                    if good["price"]*choose_coupon["discount"] <= money:
                        shopping_cart.append(good)
                        print(f"{good['name']}成功添加到购物车！")
                        money-=good["price"]*choose_coupon["discount"]
                        print(f"恭喜，购买成功！您的卡余额还剩{money}")
                    else:
                        print("穷鬼，钱不够！请到其他超市购买")
                else:
                    if good["price"]<=money:
                        shopping_cart.append(good)
                        print(f"{good['name']}成功添加到购物车！")
                        money-=good["price"]
                        print(f"恭喜，购买成功！您的卡余额还剩{money}")
                    else:
                        print("穷鬼，钱不够！请到其他超市购买")
            else:
                print("没有这个商品，别瞎弄！")
        except ValueError:
            print("无效输入，再瞎搞让你飞出去！")
# #退货系统（退货系统测试版，不用看）
# def return_item(item_name):
#     global money
#     global x
#     for item in shopping_cart:
#         if item["name"]==choose_coupon["discount"]:
#             shopping_cart.remove(item)
#             money+=item["price"]*choose_coupon["discount"]
#             print("已退回，下次不许你这样了")
#             x+=1
#             item_name = []
#             return True
#         elif item["name"] == item_name:
#             shopping_cart.remove(item)
#             money+=item["price"]
#             x+=1
#             item_name = []
#             print("已退回，下次不许你这样了")
#             return True
#         else:
#             print("别瞎搞，再搞让你飞起来")
#             return False
#优惠券系统
coupons=[{"name":"卫生纸","discount":0.8},{"name":"矿泉水","discount":0.9},{"name":"洗发水","discount":0.8},{"name":"暖水壶","discount":0.7}, {"name":"雷碧","discount":0.9},
        {"name":"康帅傅方便面","discount":0.9},{"name":"奶龙","discount":0.2},{"name":"冰箱","discount":0.85},{"name":"五粮液","discount":0.8},{"name":"衣架","discount":0.85}]
choose_coupon=random.choice(coupons)
print(f"哎呦~不错哦,抽到了{choose_coupon['name']}的{int(choose_coupon['discount']*100)}%优惠券")

#运行主系统
shopping()
#退货系统
if shopping_cart!=[]:
    return_choice=input("是否需要退货？输入'y'需要，输入'n'不需要")
    if return_choice.lower()=="y":
        item_numbers=int(input("请输入退货数："))
        x=1
        while x<=item_numbers:
            item_name=[]
            item_name=input("请输入商品名：")
            # return_item(item_name)
            for item in shopping_cart:
                if item["name"] == choose_coupon["discount"]:
                    shopping_cart.remove(item)
                    money += item["price"] * choose_coupon["discount"]
                    print("已退回，下次不许你这样了")
                    x += 1
                    item_name = []
                elif item["name"] == item_name:
                    shopping_cart.remove(item)
                    money += item["price"]
                    x += 1
                    item_name = []
                    print("已退回，下次不许你这样了")
                # else:
                #     print("别瞎搞，再搞让你飞起来")

if shopping_cart!=[]:
    receipt()















